package efr.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import efr.dao.AutorDao;
import efr.dao.EditorialDao;
import efr.dao.impl.AutorDaoImpl;
import efr.dao.impl.EditorialDaoImpl;
import efr.entity.Autor;
import efr.entity.Editorial;
import efr.util.EfrAdmUtil;

/**
 * Servlet implementation class Op4EditorialSubmitServlet
 */
public class Op4EditorialSubmitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(Op4EditorialSubmitServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Op4EditorialSubmitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		logger.debug("start doGet()");
		if("Modifica".equals(request.getParameter("action"))){
			String id = request.getParameter("id");
			String titlu = request.getParameter("titlu");
			String idAutor = request.getParameter("autor");
			String continut = request.getParameter("continut");
			String data = request.getParameter("data");
			String editorialDefault = request.getParameter("editorialDefault");
			
			Editorial editorial = new Editorial();
			editorial.setId(Integer.valueOf(id));
			editorial.setTitlu(titlu);
			editorial.setContinut(continut);
			editorial.setEditorialDefault(Integer.valueOf(editorialDefault));
			editorial.setData(EfrAdmUtil.convertStringToDate(data, "yyyy-MM-dd HH:mm:ss.S"));//2013-07-07 17:55:32.0
			
			AutorDao autorDao = new AutorDaoImpl();
			Autor autor = autorDao.getAutor(Integer.valueOf(idAutor));	
			editorial.setAutor(autor);
			
			EditorialDao editorialDao = new EditorialDaoImpl();
			editorialDao.updatedEditorial(editorial);
			
		}
		
		if("Adauga".equals(request.getParameter("action"))){
			String titlu = request.getParameter("titlu");
			String idAutor = request.getParameter("autor");
			String continut = request.getParameter("continut");
			String data = request.getParameter("data");
			String editorialDefault = request.getParameter("editorialDefault");
			
			Editorial editorial = new Editorial();
			editorial.setTitlu(titlu);
			editorial.setContinut(continut);
			editorial.setEditorialDefault(Integer.valueOf(editorialDefault));
			editorial.setData(EfrAdmUtil.convertStringToDate(data, "yyyy-MM-dd HH:mm:ss.S"));//2013-07-07 17:55:32.0
			
			AutorDao autorDao = new AutorDaoImpl();
			Autor autor = autorDao.getAutor(Integer.valueOf(idAutor));	
			editorial.setAutor(autor);
			
			EditorialDao editorialDao = new EditorialDaoImpl();
			editorialDao.addEditorial(editorial);
			
		}
		
		RequestDispatcher view = request.getRequestDispatcher("/ListEditoriale.do");
		logger.debug("end doGet()");
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
